package com.wanchuang.shengfubao.module.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.adapter.AccountAdapter;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.base.MainApplication;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.Recharge;
import com.wanchuang.shengfubao.utils.GsonUtils;
import com.wanchuang.shengfubao.utils.ValidateUtils;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AccountActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_message)
    TextView tvMessage;

    private AccountAdapter mAdapter;
    private List<Recharge.Tab> tabList;

    private String isCash;
    private String errorMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
        setContentView(R.layout.activity_account);
        ButterKnife.bind(this);
        tvTitle.setText("我的账户");
        tvTitleRight.setText("提现记录");
        tvTitleRight.setVisibility(View.GONE);
        tvMessage.setMovementMethod(new ScrollingMovementMethod());
        initRecyclerView();
        initListener();
        loadData();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());
        tvTitleRight.setOnClickListener(v -> {
            Intent intent = new Intent(this, TiXianProgressActivity.class);
            intent.setType(GsonUtils.serializedToJson(mAdapter.getData()));
            startActivity(intent);
        });

    }

    private void loadData() {
        showProgress();
        NetworkManager.INSTANCE.post(Apis.my_earnings, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                hideProgress();
                Recharge.My_earnings data = Recharge.My_earnings.parseFrom(response);
                List<Recharge.My_earnings.wallet_list> lists = data.getWalletArrayList();
                List<Recharge.My_earnings.Describe> list_describe = data.getDesListList();
                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list_describe.size() - 1; i++) {
                    des.append(list_describe.get(i).getDes()).append("\n");
                }
                des.append(list_describe.get(list_describe.size() - 1).getDes());
                tvMessage.setText(des.toString());
                tabList = data.getTabListList();
                Log.e("data", "onOk: " + lists.toString());
                isCash = data.getIsCash();
                errorMessage = data.getErrorCashMsg();
                mAdapter.setNewInstance(lists);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (MainApplication.isRef) {
            MainApplication.isRef = false;
            loadData();
        }
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new AccountAdapter(mContext));

        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(this, TiXianActivity.class);
            intent.setType(mAdapter.getItem(position).getWalletId());
            intent.putExtra("isCash", isCash);
            intent.putExtra("errorMessage", errorMessage);
            if (ValidateUtils.isValidate(mAdapter.getItem(position).getAvailable())) {
                intent.putExtra("yu_er", mAdapter.getItem(position).getAvailable());
            } else {
                intent.putExtra("yu_er", mAdapter.getItem(position).getBalance());
            }
            intent.setAction(mAdapter.getItem(position).getWalletName());
            startActivity(intent);
        });
        mAdapter.addChildClickViewIds(R.id.tv_zhan);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_zhan) {
                Intent intent1 = new Intent(mContext, ZhangDanActivity.class);
                for (Recharge.Tab tab : tabList) {
                    if (tab.getWalletType().equals(mAdapter.getData().get(position).getWalletId())) {
                        intent1.putExtra("tab", GsonUtils.serializedToJson(tab));
                        break;
                    }
                }
                intent1.setType(mAdapter.getData().get(position).getWalletName());
                startActivity(intent1);
            }
        });
    }

}
